package com.developer.t24haber.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.developer.t24haber.R;
import com.developer.t24haber.activity.NewsActivity;
import com.developer.t24haber.model.News;
import com.developer.t24haber.util.NewsDownloader;
import com.developer.t24haber.util.Util;

public class MainFragment extends ListFragment {

	public static final String TAG = MainFragment.class.getName();

	private static final String XML_ADDRESS = "http://t24.com.tr/rss";

	private NewsDownloader newsDownloader;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (Util.getInstance(getActivity()).isInternetAvailable()) {
			newsDownloader = new NewsDownloader(this);
			newsDownloader.execute(XML_ADDRESS);
		} else {
			Toast.makeText(getActivity(), R.string.connection_error, Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		News news = newsDownloader.getNews().get(position);
		Intent intent = new Intent(getActivity(), NewsActivity.class);
		intent.putExtra(NewsActivity.EXTRA_NEWS_LINK, news.getLink());
		startActivity(intent);
		super.onListItemClick(l, v, position, id);
	}

}
